﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Text.RegularExpressions;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
namespace Tracy.CoreLib.Data
{
    public class Filter : XmlModel_AutoParseFunctions, IXmlModel 
    {
        #region 构造函数
        public Filter() : base() { }
        public Filter(string xml) : base(xml) { }
        public Filter(XmlNode node) : base(node) { }
        #endregion

        public string XmlNodeName
        {
            get { return Constants.CONST_TAG_FILTER; }
        }

        private Regex regexpr;
        /// <summary>
        /// 正则表达式对象
        /// </summary>
        public Regex Regexpr
        {
            get { if (regexpr == null) regexpr = new Regex(FilterExpr); return regexpr; }
        }

        //private string filterExpr;

        //public string FilterExpr
        //{
        //    get { return filterExpr; }
        //    set { 
        //        filterExpr = value;
        //        regexpr = null;
        //    }
        //}

        public string FilterExpr
        {
            get
            {
                if (Data.ContainsKey(Constants.CONST_TAG_FILTEREXPR)) return Data[Constants.CONST_TAG_FILTEREXPR];
                else return "";
            }
            set
            {
                Data[Constants.CONST_TAG_FILTEREXPR] = value;
                regexpr = null;
            }
        }


        private int leftPos = 0;

        public int LeftPos
        {
            get { return leftPos; }
            set
            {
                if (leftPos != value)
                {
                    leftPos = value;
                    Data[Constants.CONST_TAG_LEFTPOS] = value.ToString();
                }
            }
        }

        private int rightPos = 0;

        public int RightPos
        {
            get { return rightPos; }
            set
            {
                if (rightPos != value)
                {
                    rightPos = value;
                    Data[Constants.CONST_TAG_RIGHTPOS] = value.ToString();
                }
            }
        }

        //protected override void PostGenerateXml(XmlDocument doc, XmlNode node)
        //{
        //    if (LeftPos > 0) AppendAttribute(doc, node, Constants.CONST_TAG_LEFTPOS, LeftPos.ToString());
        //    if (RightPos > 0) AppendAttribute(doc, node, Constants.CONST_TAG_RIGHTPOS, RightPos.ToString());
        //    node.InnerText = FilterExpr;
        //}

        protected override void PreParseXml(XmlNode node)
        {
            XmlNode tmpNode = SelectNode(node, Constants.CONST_TAG_FILTEREXPR, false);
            if (tmpNode != null)
            {
                FilterExpr = tmpNode.InnerText;
                tmpNode = SelectNode(node, Constants.CONST_TAG_LEFTPOS, false);
                if (tmpNode != null) LeftPos = Convert.ToInt32(tmpNode.InnerText);
                tmpNode = SelectNode(node, Constants.CONST_TAG_RIGHTPOS, false);
                if (tmpNode != null) RightPos = Convert.ToInt32(tmpNode.InnerText);
            }
            else
            {
                //兼容旧版
                FilterExpr = node.InnerText;
                XmlNode tmpAttr = SelectAttribute (node,  Constants.CONST_TAG_LEFTPOS);
                if (tmpAttr != null) LeftPos = Convert.ToInt32(tmpAttr.InnerText);
                tmpAttr = SelectAttribute(node, Constants.CONST_TAG_RIGHTPOS);
                if (tmpAttr != null) RightPos = Convert.ToInt32(tmpAttr.InnerText);
            }
            
        }

        //public StringCollection MatchAll(string input)
        //{
        //    StringCollection output = new StringCollection();
        //    CaptureCollection  captures = Regexpr.Match(input).Captures;
        //    string tmp;
        //    foreach (Capture c in captures)
        //    {
        //        tmp = c.Value;
        //        if (LeftPos > 0 || RightPos > 0) tmp = tmp.Substring(leftPos, tmp.Length - LeftPos - RightPos);
        //        output.Add(tmp);
        //    }
        //    return output;
        //}

        //public string Match(StringDictionary input)
        //{
        //    return Match(input[Target]);
        //}

        public string Match(string input)
        {
            if (input == null) return null;
            string output = Regexpr.Match(input).Value;
            if (output != null && output != "" && (LeftPos > 0 || RightPos > 0)) output = output.Substring(LeftPos, output.Length - LeftPos - RightPos);
            return output;
        }

        //public StringCollection Match(XmlNode node)
        //{
        //    StringCollection output = new StringCollection();
        //    string tmp;
        //    foreach (XmlNode targetNode in SelectNodes(node, Target))
        //    {
        //        tmp = Match(targetNode.InnerText);
        //        if (tmp != null && tmp != "") output.Add(tmp);
        //    }
        //    return output;
        //}

        //public XmlModelCollection MatchRssItem(XmlModelCollection rssItems)
        //{
        //    foreach (RssModel.RssItem item in rssItems)
        //    {

        //    }
        //}
    }
}
